package com.wsd.smartcarwasher.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class PictureUtil {

    private Graphics g= null;

    /**
     * 导入本地图片到缓冲区
     */
    public BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

    /**
     *
     * @param b 要添加的图片
     * @param d 底图
     * @return
     */
    public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {

        try {
            int w = b.getWidth();
            int h = b.getHeight();
            g = d.getGraphics();
            g.drawImage(b, d.getWidth()/2-w/2-150, d.getHeight()-1400, w*2, h*2, null);
            g.dispose();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        return d;
    }

    /**
     * 生成新图片到本地
     */
    public void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    public static void main(String[] args) {
       PictureUtil tt = new PictureUtil();
        BufferedImage d = tt.loadImageLocal("E:/1.jpg");
        BufferedImage b = tt.loadImageLocal("E:/11.jpeg");
        tt.writeImageLocal("E:/new10.jpg", tt.modifyImagetogeter(b, d));
        /* //将多张图片合在一起
        /*System.out.println("success");

        String str ="http://mwxc.rungyun.cn:8091/car-wash.e401d259.jpg";//)

        System.out.println( Constant.USE_DIR+str.substring(str.lastIndexOf("/")+1));


        Date endTime =DateUtil.getDateLater(new Date(), 30 );
        if(endTime.after(new Date())){
            System.out.println("====");
        }*/
    }
}
